package com.xuecheng.order.controller;

import com.alibaba.excel.EasyExcel;
import com.xuecheng.commons.model.vo.OrdersVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.UserOrdersVo;
import com.xuecheng.order.domain.OrdersExcel;
import com.xuecheng.order.service.OrdersService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 订单前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService  ordersService;

    /**
     * 创建订单
     * 请求接口：POST /create/{coursePubId}
     * 参数：课程发布id，coursePubId
     * 响应数据：ResponseResult<String>
     */
    @PostMapping("/create/{coursePubId}")
    public ResponseResult<String> create(@PathVariable("coursePubId") Long coursePubId){
        return ordersService.create(coursePubId);
    }

    /**
     * 根据orderNo查询，订单的支付状态
     * GET  /{orderNo}
     * 参数：orderNo（订单编号）
     * 返回值：ResponseResult<OrderVo>
     */
    @GetMapping("/{orderNo}")
    public ResponseResult<OrdersVo> queryOrders(@PathVariable("orderNo") String orderNo) {
        return ordersService.queryOrders(orderNo);
    }
    @GetMapping("/project/57/interface/api/1352")
    public ResponseResult<List<UserOrdersVo>> selectUserOrder(@RequestParam("id") String id){
        return ordersService.selectUserOrder(id);
    }
    @GetMapping("project/57/interface/api/1387")
    public ResponseResult<List> selectOrder(){
        return ordersService.selectOrder();
    }
    @SneakyThrows
    @GetMapping("orders/order/excel/{companyId}")
    public ResponseResult excelDownload(HttpServletResponse httpServletResponse,@PathVariable("companyId") String companyId){
        ResponseResult<List<OrdersExcel>> inputStream = ordersService.excelDownload(httpServletResponse,companyId);
        EasyExcel.write(httpServletResponse.getOutputStream(), OrdersExcel.class).sheet("模板").doWrite(inputStream.getData());
        return inputStream;
    }

}
